﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Reconciliation.Data
{
    public partial class InvoiceAuditorComment
    {
        public static Domain.InvoiceAuditorComment MapDataToDomain(Data.InvoiceAuditorComment dataInvoiceAuditorComment)
        {
            Domain.InvoiceAuditorComment domainInvoiceAuditorComment = new Reconciliation.Domain.InvoiceAuditorComment();
            domainInvoiceAuditorComment.Comment = dataInvoiceAuditorComment.Comment;
            domainInvoiceAuditorComment.InvoiceAuditorCommentID = dataInvoiceAuditorComment.InvoiceAuditorCommentID;
            domainInvoiceAuditorComment.User = User.MapDataToDomain(dataInvoiceAuditorComment.User);
            return domainInvoiceAuditorComment;
        }

        public static Data.InvoiceAuditorComment MapDomainToData(Domain.InvoiceAuditorComment domainInvoiceAuditorComment)
        {
            Data.InvoiceAuditorComment dataInvoiceAuditorComment = new Reconciliation.Data.InvoiceAuditorComment();
            dataInvoiceAuditorComment.Comment = domainInvoiceAuditorComment.Comment;
            dataInvoiceAuditorComment.InvoiceReference.EntityKey = new System.Data.EntityKey("ReconciliationEntities.Invoice", "InvoiceID", domainInvoiceAuditorComment.Invoice.InvoiceID);
            dataInvoiceAuditorComment.UserReference.EntityKey = new System.Data.EntityKey("ReconciliationEntities.User", "UserID", domainInvoiceAuditorComment.Invoice.InvoiceID);
            return dataInvoiceAuditorComment;
        }

        public static void SaveInvoiceComment(Domain.InvoiceAuditorComment domainInvoiceAuthorComment)
        {
            try
            {
                using (ReconciliationEntities recEnt = new ReconciliationEntities())
                {
                    Data.InvoiceAuditorComment dataInvoiceAuditorComment = Data.InvoiceAuditorComment.MapDomainToData(domainInvoiceAuthorComment);
                    recEnt.AddToInvoiceAuditorComment(dataInvoiceAuditorComment);
                    recEnt.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.LogEntryOnException("SaveInvoiceComment", ex);
            }
        }

        public static List<Domain.InvoiceAuditorComment> GetListOfInvoiceAuditorComment(int invoiceID)
        {
            try
            {
                List<Domain.InvoiceAuditorComment> domainInvoiceAuditorCommentList = new List<Reconciliation.Domain.InvoiceAuditorComment>();
                Domain.InvoiceAuditorComment domainInvoiceAuditorComment = null;
                ReconciliationEntities recEnt = new ReconciliationEntities();
                List<Data.InvoiceAuditorComment> dataInvoiceAuditorComment = recEnt.InvoiceAuditorComment.Include("User").Where(i => i.Invoice.InvoiceID.Equals(invoiceID)).ToList();
                foreach (Data.InvoiceAuditorComment iac in dataInvoiceAuditorComment)
                {
                    domainInvoiceAuditorComment = MapDataToDomain(iac);
                    domainInvoiceAuditorCommentList.Add(domainInvoiceAuditorComment);
                }
                return domainInvoiceAuditorCommentList;
            }
            catch (Exception ex)
            {
                Log.LogEntryOnException("GetListOfInvoiceAuditorComment", ex);
                return new List<Reconciliation.Domain.InvoiceAuditorComment>();
            }
        }
    }
}
